{% extends 'base.html' %}
{% load static %}

{% block title %}资产报表{% endblock %}

{% block extra_css %}
<style>
    .stats-card {
        border-left: 4px solid #007bff;
        transition: transform 0.2s;
    }
    .stats-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    .stats-number {
        font-size: 2rem;
        font-weight: bold;
        color: #007bff;
    }
    .stats-label {
        color: #6c757d;
        font-size: 0.9rem;
    }
    .filter-section {
        background: #f8f9fa;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    .table-responsive {
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .status-badge {
        font-size: 0.8rem;
        padding: 4px 8px;
    }
    .chart-container {
        width: 100%;
        height: 400px;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-chart-bar me-2"></i>资产报表</h2>
                <div class="btn-group">
                    <a href="{% url 'assets:export_asset_report_excel' %}{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}" 
                       class="btn btn-success">
                        <i class="fas fa-file-excel me-1"></i>导出Excel
                    </a>
                </div>
            </div>

            <!-- 筛选区域 -->
            <div class="filter-section">
                <form method="get" class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">资产类别</label>
                        <select name="category" class="form-select">
                            <option value="">全部类别</option>
                            {% for category in categories %}
                                <option value="{{ category.id }}" 
                                        {% if category.id|stringformat:"s" == selected_category %}selected{% endif %}>
                                    {{ category.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">资产状态</label>
                        <select name="status" class="form-select">
                            <option value="">全部状态</option>
                            {% for status_code, status_name in status_choices %}
                                <option value="{{ status_code }}" 
                                        {% if status_code == selected_status %}selected{% endif %}>
                                    {{ status_name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">所属部门</label>
                        <select name="department" class="form-select">
                            <option value="">全部部门</option>
                            {% for department in departments %}
                                <option value="{{ department.id }}" 
                                        {% if department.id|stringformat:"s" == selected_department %}selected{% endif %}>
                                    {{ department.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">开始日期</label>
                        <input type="date" name="start_date" class="form-control" value="{{ selected_start_date }}">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">结束日期</label>
                        <input type="date" name="end_date" class="form-control" value="{{ selected_end_date }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">关键词搜索</label>
                        <div class="input-group">
                            <input type="text" name="search" class="form-control" 
                                   placeholder="资产名称、编号..." value="{{ search_query }}">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i>
                            </button>
                        </div>
                    </div>
                </form>
            </div>

            <!-- 统计卡片 -->
            <div class="row mb-4">
                <div class="col-md-2">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number">{{ total_assets }}</div>
                            <div class="stats-label">资产总数</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-success">{{ in_use_count }}</div>
                            <div class="stats-label">使用中</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-info">{{ idle_count }}</div>
                            <div class="stats-label">闲置</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-warning">{{ maintenance_count }}</div>
                            <div class="stats-label">维修中</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-danger">{{ faulty_count }}</div>
                            <div class="stats-label">故障</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-primary">¥{{ total_value|floatformat:2 }}</div>
                            <div class="stats-label">总价值</div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 可视化图表 -->
            <div class="row mb-4">
                <div class="col-lg-4 mb-4">
                    <div class="card h-100">
                        <div class="card-header"><i class="fas fa-pie-chart me-2"></i>资产状态分布</div>
                        <div class="card-body">
                            <div id="status-chart" class="chart-container"></div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4 mb-4">
                    <div class="card h-100">
                        <div class="card-header"><i class="fas fa-tags me-2"></i>资产类别分布 (Top 10)</div>
                        <div class="card-body">
                            <div id="category-chart" class="chart-container"></div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4 mb-4">
                    <div class="card h-100">
                        <div class="card-header"><i class="fas fa-building me-2"></i>部门资产分布 (Top 10)</div>
                        <div class="card-body">
                            <div id="department-chart" class="chart-container"></div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 资产列表 -->
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i>资产详情列表</h5>
                    <small class="text-muted">共 {{ total_assets }} 条记录</small>
                </div>
                <div class="card-body p-0">
                    {% if assets %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>资产编号</th>
                                    <th>资产名称</th>
                                    <th>类别</th>
                                    <th>状态</th>
                                    <th>所属部门</th>
                                    <th>使用人</th>
                                    <th>购买价格</th>
                                    <th>启用日期</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for asset in assets %}
                                <tr>
                                    <td>
                                        <a href="{% url 'assets:asset_detail' asset.id %}" 
                                           class="text-decoration-none">
                                            {{ asset.asset_number }}
                                        </a>
                                    </td>
                                    <td>{{ asset.name }}</td>
                                    <td>
                                        {% if asset.category %}
                                            {{ asset.category.name }}
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if asset.status == 'in_use' %}
                                            <span class="badge bg-success status-badge">使用中</span>
                                        {% elif asset.status == 'idle' %}
                                            <span class="badge bg-info status-badge">闲置</span>
                                        {% elif asset.status == 'maintenance' %}
                                            <span class="badge bg-warning status-badge">维修中</span>
                                        {% elif asset.status == 'borrowed' %}
                                            <span class="badge bg-primary status-badge">借用中</span>
                                        {% elif asset.status == 'faulty' %}
                                            <span class="badge bg-danger status-badge">故障</span>
                                        {% elif asset.status == 'scrapped' %}
                                            <span class="badge bg-secondary status-badge">报废</span>
                                        {% else %}
                                            <span class="badge bg-light text-dark status-badge">{{ asset.get_status_display }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if asset.department %}
                                            {{ asset.department.name }}
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if asset.user %}
                                            {{ asset.user.get_full_name|default:asset.user.username }}
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    <td>¥{{ asset.purchase_price|floatformat:2|default:"-" }}</td>
                                    <td>{{ asset.purchase_date|date:"Y-m-d"|default:"-" }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <p class="text-muted">暂无符合条件的资产记录</p>
                        {% if search_query or selected_category or selected_status or selected_department %}
                        <a href="{% url 'assets:asset_report' %}" class="btn btn-outline-primary">
                            <i class="fas fa-times me-1"></i>清除筛选条件
                        </a>
                        {% endif %}
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.3/dist/echarts.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 自动提交筛选表单
    const selects = document.querySelectorAll('select[name="category"], select[name="status"], select[name="department"]');
    selects.forEach(select => {
        select.addEventListener('change', function() {
            this.form.submit();
        });
    });

    // 解析Django传来的JSON数据
    const statusData = JSON.parse('{{ status_stats_json|escapejs }}');
    const categoryData = JSON.parse('{{ category_stats_json|escapejs }}');
    const departmentData = JSON.parse('{{ department_stats_json|escapejs }}');
    const statusChoices = JSON.parse('{{ status_choices_json|escapejs }}');

    // 初始化图表
    const statusChart = echarts.init(document.getElementById('status-chart'));
    const categoryChart = echarts.init(document.getElementById('category-chart'));
    const departmentChart = echarts.init(document.getElementById('department-chart'));

    // 1. 资产状态分布 - 饼图
    if (statusData.length > 0) {
        const statusChartOption = {
            tooltip: { trigger: 'item' },
            legend: { top: 'bottom', left: 'center' },
            series: [{
                name: '资产状态',
                type: 'pie',
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: { show: false, position: 'center' },
                emphasis: {
                    label: { show: true, fontSize: '20', fontWeight: 'bold' }
                },
                labelLine: { show: false },
                data: statusData.map(item => ({
                    value: item.count,
                    name: statusChoices[item.status] || item.status
                }))
            }]
        };
        statusChart.setOption(statusChartOption);
    }

    // 2. 资产类别分布 - 条形图
    if (categoryData.length > 0) {
        const categoryChartOption = {
            tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } },
            grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
            xAxis: { type: 'value', boundaryGap: [0, 0.01] },
            yAxis: { 
                type: 'category', 
                data: categoryData.map(item => item.category__name).reverse(),
                axisLabel: {
                    interval: 0,
                    rotate: 0,
                    formatter: function (value) {
                        return value.length > 10 ? value.substring(0, 10) + '...' : value;
                    }
                }
            },
            series: [{
                name: '资产数量',
                type: 'bar',
                data: categoryData.map(item => item.count).reverse()
            }]
        };
        categoryChart.setOption(categoryChartOption);
    }

    // 3. 部门资产分布 - 条形图
    if (departmentData.length > 0) {
        const departmentChartOption = {
            tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } },
            grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
            xAxis: { type: 'value', boundaryGap: [0, 0.01] },
            yAxis: { 
                type: 'category', 
                data: departmentData.map(item => item.department__name).reverse(),
                axisLabel: {
                    interval: 0,
                    rotate: 0,
                    formatter: function (value) {
                        return value.length > 10 ? value.substring(0, 10) + '...' : value;
                    }
                }
            },
            series: [{
                name: '资产数量',
                type: 'bar',
                data: departmentData.map(item => item.count).reverse()
            }]
        };
        departmentChart.setOption(departmentChartOption);
    }

    // 使图表响应式
    window.addEventListener('resize', function() {
        statusChart.resize();
        categoryChart.resize();
        departmentChart.resize();
    });
});
</script>
{% endblock %}